package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.dao.DrugLibraryDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.DrugLibrary;
import com.itheima.service.DrugLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = DrugLibraryService.class)
@Transactional
public class DrugLibraryServiceImpl implements DrugLibraryService {

    @Autowired
    private DrugLibraryDao drugLibraryDao;
    @Override
    public void add(List<DrugLibrary> drugLibraryList) {

        if (drugLibraryList!=null &&drugLibraryList.size()>0){
            for (DrugLibrary drugLibrary : drugLibraryList) {
                String name = drugLibraryDao.findDrugName(drugLibrary.getDrugname());
                if (name==null){
                    drugLibraryDao.add(drugLibrary);
                }
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> pageInfo = drugLibraryDao.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getResult());
        return pageResult;
    }

    @Override
    public List<DrugLibrary> findAll() {
        return drugLibraryDao.findAll();
    }

    @Override
    public void insert(DrugLibrary drugLibrary) {
        drugLibraryDao.insert(drugLibrary);

    }
}
